package com.samoiloff.util.actions {
	import flash.events.ErrorEvent;
	import flash.events.SecurityErrorEvent;
	import com.samoiloff.util.actions.Action;

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * @author Samoiloff  http://samoiloff.com/
	 */
	public class LoadTextActionItem extends Action {
		
		protected var _urlRequest : URLRequest;
		protected var _urlLoader:URLLoader;
		
		public function LoadTextActionItem() {
			super();
		}
		
		override public function start() : void {
			super.start();
			_urlLoader = new URLLoader(_urlRequest);
			_urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
		}

		protected function onError(event : ErrorEvent) : void {
			throw new Error("this function should be overriden");
		}

		protected function onLoadComplete(event:Event) : void {
			throw new Error("this function should be overrriden");
		}

		
		override public function destroy() : void {
			_urlRequest = null;
			if (_urlLoader) {
				_urlLoader.removeEventListener(Event.COMPLETE, onLoadComplete);
				_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
				_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
				
				_urlLoader = null;
			}
			super.destroy();
		}

	}
}
